package com.ybkj.daijia;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

public class OrderConfirmLocs {

    private static Logger logger = LoggerFactory.getLogger(OrderConfirmLocs.class);

    public static String locs(MultipartFile jsonLocFile) {
        return locs(jsonLocFile, null);
    }

    public static String locs(MultipartFile jsonLocFile, String fileName) {
        InputStreamReader read = null;
        BufferedReader bufferedReader = null;
        StringBuilder sb = new StringBuilder();
        PrintWriter pw = null;
        String jsonLocs = "";
        try {
            read = new InputStreamReader(jsonLocFile.getInputStream());
            bufferedReader = new BufferedReader(read);
            if (fileName != null) {
                pw = new PrintWriter("/opt/tomcat/logs/locations/" + fileName);
            }
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                sb.append(lineTxt);
                if (fileName != null) {
                    pw.println(lineTxt);
                }
            }
            jsonLocs = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != read) {
                try {
                    read.close();
                    if (pw != null) {
                        pw.close();
                    }
                } catch (IOException e) {

                    logger.error("exeception {}", e);
                }
            }
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {

                    logger.error("exeception {}", e);
                }
            }
        }
        return jsonLocs;
    }

}
